<template>
  <div>
    <router-link to="role/add">
      <el-button>添加</el-button>
    </router-link>

    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="id"
      lazy
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="ID" prop="id"></el-table-column>

      <el-table-column label="角色名称" prop="rolename"> </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            circle
            @click="edit(scope.row.id)"
            icon="el-icon-edit"
            size="mini"
          ></el-button>

          <el-button
            type="warning"
            circle
            @click="view(scope.row.id)"
            icon="el-icon-view"
            size="mini"
          ></el-button>

          <el-button
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
            size="mini"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="角色信息" :visible.sync="dialogVisible" width="40%">
      <el-tree
        :data="menuList"
        show-checkbox
        ref="tree"
        node-key="id"
        :default-expanded-keys="[2, 3]"
        :default-checked-keys="[5]"
        :props="defaultProps"
      >
      </el-tree>
    </el-dialog>
  </div>
</template>

<style>
</style>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      menuList: [],
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  created() {
    this.http.get("rolelist").then((res) => {
      this.tableData = res.data.list;
    });
    this.getMenuData()


    // 获取树形控件的内容

  },
  methods: {
    getMenuData() {
      this.http('/menulist',{params: {istree: 1}})
        .then(res => {
          this.menuList = res.data.list;
        })
    },
    // 编辑
    edit(id) {
      this.$router.push("/layout/role/" + id);
    },
    // 删除，需要二次确认
    handleDelete(id) {
      this.$confirm("确认关闭？")
        .then((_) => {
          //  确认删除
          this.doDel(id);
        })
        .catch((_) => {
          //  关闭弹窗
        });
    },
    doDel(id) {
      this.http.post("/roledelete", { id: id }).then((res) => {
        this.tableData = res.list;
      });
    },
    getDataById(id) {
      this.http
        .get("/roleinfo", { params: { id } })
        .then((res) => {
  
          this.form = res.data.list;
          this.form.menus = res.data.list.menus.split(",");

          this.$refs.tree.setCheckedKeys(this.form.menus);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    view(id) {
      this.getDataById(id);
      this.dialogVisible = true;
    },
  },
};
</script>